package io.oschina.binv.kapp

import android.app.Activity
import android.content.ContentValues
import android.content.Context
import android.content.Intent
import android.net.Uri
import android.os.Build
import android.os.Environment
import android.provider.MediaStore

import com.yalantis.ucrop.UCrop

import java.io.File
import java.io.FileNotFoundException

/**
 * by y on 17/07/2017.
 *
 *
 * 使用相机拍照并裁剪
 */

class SimpleRxGalleryFinal {

    private var listener: RxGalleryFinalCropListener? = null

    private var imagePath: Uri? = null





    fun init(listener: RxGalleryFinalCropListener): SimpleRxGalleryFinal {
        this.listener = listener
        return this
    }


    fun openCamera() {
        val intent = Intent(MediaStore.ACTION_IMAGE_CAPTURE)
        imagePath = Uri.fromFile(diskCacheDir)
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.N) {
            intent.putExtra(MediaStore.EXTRA_OUTPUT, imagePath)
        } else {
            val contentValues = ContentValues(1)
            contentValues.put(MediaStore.Images.Media.DATA, imagePath!!.path)
            contentValues.put(MediaStore.Images.Media.MIME_TYPE, IMAGE_TYPE)
            val uri = listener!!.simpleActivity.contentResolver.insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, contentValues)
            intent.putExtra(MediaStore.EXTRA_OUTPUT, uri)
        }
        listener!!
                .simpleActivity
                .startActivityForResult(intent, TYPE_CAMERA)
    }

    fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        when (resultCode) {
            Activity.RESULT_CANCELED -> listener!!.onCropCancel()
            UCrop.RESULT_ERROR -> if (data != null) {
                val cropError = UCrop.getError(data)
                if (cropError != null) {
                    listener!!.onCropError(cropError.message!!)
                } else {
                    listener!!.onCropError("裁剪出现未知错误")
                }
            } else {
                listener!!.onCropError("获取相册图片出现错误")
            }

            Activity.RESULT_OK -> when (requestCode) {
                TYPE_CAMERA -> {
                    notifyImageToCamera(listener!!.simpleActivity, imagePath)
                    val of = UCrop.of(imagePath!!, Uri.fromFile(diskCacheDir))
                    of.start(listener!!.simpleActivity)
                }
                UCrop.REQUEST_CROP -> listener!!.onCropSuccess(UCrop.getOutput(data!!))
            }
        }
    }

    private val diskCacheDir: File
        get() {
            val cachePath: String
            if (Environment.MEDIA_MOUNTED == Environment.getExternalStorageState() || !Environment.isExternalStorageRemovable()) {
                val externalCacheDir = listener!!.simpleActivity.externalCacheDir
                if (externalCacheDir != null) {
                    cachePath = externalCacheDir.path
                } else {
                    cachePath = listener!!.simpleActivity.cacheDir.path
                }
            } else {
                cachePath = listener!!.simpleActivity.cacheDir.path
            }
            return File(cachePath, imageName())
        }

    private fun notifyImageToCamera(context: Context, uri: Uri?) {
        try {
            val file = File(uri!!.path)
            MediaStore.Images.Media.insertImage(context.contentResolver, file.absolutePath, file.name, null)
        } catch (e: FileNotFoundException) {
            e.printStackTrace()
        }

        context.sendBroadcast(Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, uri))
    }

    private fun imageName(): String {
        return System.currentTimeMillis().toString() + ".jpg"
    }


    interface RxGalleryFinalCropListener {

        val simpleActivity: Activity


        /**
         * 裁剪被取消
         */
        fun onCropCancel()

        /**
         * 裁剪成功
         *
         * @param uri 裁剪的 Uri , 有可能会为Null
         */
        fun onCropSuccess(uri: Uri?)


        /**
         * 裁剪失败
         *
         * @param errorMessage 错误信息
         */
        fun onCropError(errorMessage: String)

    }

    companion object {

        private val IMAGE_TYPE = "image/jpeg"
        private val TYPE_CAMERA = 1111
        private object SimpleRxGalleryFinalHolder {
            val SIMPLE_RX_GALLERY_FINAL = SimpleRxGalleryFinal()
        }
        fun get(): SimpleRxGalleryFinal {
            return SimpleRxGalleryFinalHolder.SIMPLE_RX_GALLERY_FINAL
        }
    }
}
